import { KoaMiddlewareInterface } from 'routing-controllers';
import { Log } from '@/utils/log';
import  moment from 'moment';

export class RequestLog implements KoaMiddlewareInterface {
  async use(context: any, next: (err?: any) => Promise<any>): Promise<any> {
    const start = new Date().valueOf()
    let nextAction = await next()
    const ms = new Date().valueOf() - start


    let message: string = `\n1 请求基本信息: ${context.request.method} ${ms}ms ${context.request.url} ${context.request.ip} ${moment().format('YYYY-MM-DD HH:mm:ss')}\n2 请求参数: ${JSON.stringify(
        context.request.method == "GET" 
        ? context.request.querystring
        : context.request.body
    )} \n3 请求头: ${JSON.stringify(context.request.header)} \n\n`;

    console.log(message)
    Log.ApplicationLogger().warn(message)
    
    return nextAction

  }
}